package com.hangwei.paperless;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.paperless.adapter.CardStepAdapter;
import com.hangwei.paperless.adapter.HorizontalPicAdapter;
import com.hangwei.paperless.adapter.SymbolAdapter;
import com.hangwei.paperless.model.bean.PartsRoutingSheet;
import com.hangwei.paperless.model.bean.ProcessSheet;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.twelve.all.base.BaseActivity;
import com.twelve.all.net.API;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.Tools;

import java.util.ArrayList;

public class CardProcessActivity extends BaseActivity {

    private LoadingDialog mLoadingDialog;
    private String mUserId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_process);
        ProcessSheet info = (ProcessSheet) getIntent().getSerializableExtra("info");
        findViewById(R.id.back).setOnClickListener(v -> finish());
        TextView tvBatchNo = (TextView) findViewById(R.id.tvBatchNo);
        TextView tvWoNo = (TextView) findViewById(R.id.tvWoNo);
        TextView tvIPL = (TextView) findViewById(R.id.tvIPL);
        TextView tvMaterial = (TextView) findViewById(R.id.tvMaterial);
        TextView tvCmmPN = (TextView) findViewById(R.id.tvCmmPN);
        TextView tvPN = (TextView) findViewById(R.id.tvPN);
        TextView tvSN = (TextView) findViewById(R.id.tvSN);
        TextView tvRevNum = (TextView) findViewById(R.id.tvRevNum);
//        TextView tvProduct = (TextView) findViewById(R.id.tvProduct);
//        TextView tvWoNo = (TextView) findViewById(R.id.tvWoNo);
        TextView tvNum = (TextView) findViewById(R.id.tvNum);
        TextView tvDes = (TextView) findViewById(R.id.tvDes);
        TextView tvRevVersion = (TextView) findViewById(R.id.tvRevVersion);
        TextView tvPreparedByDate = (TextView) findViewById(R.id.tvPreparedByDate);
        TextView tvCheckedByData = (TextView) findViewById(R.id.tvCheckedByData);
        TextView tvApprovedByDate = (TextView) findViewById(R.id.tvApprovedByDate);
        SimpleDraweeView sdvPreparedBySign =findViewById(R.id.sdvPreparedBySign);
        SimpleDraweeView sdvCheckedBySign = findViewById(R.id.sdvCheckedBySign);
        SimpleDraweeView sdvApprovedBySign = findViewById(R.id.sdvApprovedBySign);
//        ImageView ivApproved = findViewById(R.id.ivApproved);
//        ImageView ivPrepared = findViewById(R.id.ivPrepared);
//        ImageView ivChecked = findViewById(R.id.ivChecked);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layout);
        CardStepAdapter adapter = new CardStepAdapter(null);
        recyclerView.setAdapter(adapter);

        RecyclerView recyclerViewPic = findViewById(R.id.recyclerViewPic);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewPic.setLayoutManager(layoutManager);
        HorizontalPicAdapter picAdapter = new HorizontalPicAdapter(null);
        recyclerViewPic.setAdapter(picAdapter);
        recyclerViewPic.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId()==R.id.sdv){
                    showBigPic((ArrayList<String>) adapter.getData(),position);
                }
            }
        });

        LinearLayout llKV = findViewById(R.id.llKV);
        RecyclerView recyclerViewKV = findViewById(R.id.recyclerViewKV);

        TextView tvValue0 = findViewById(R.id.tvValue0);
        TextView tvValue1 = findViewById(R.id.tvValue1);
        TextView tvValue2 = findViewById(R.id.tvValue2);
        TextView tvValue3 = findViewById(R.id.tvValue3);
        TextView tvValue4 = findViewById(R.id.tvValue4);
        TextView tvValue5 = findViewById(R.id.tvValue5);

        mLoadingDialog = new LoadingDialog(this);
        mLoadingDialog.show();
        mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getPartsRoutingSheets(mUserId,info.partsRoutingSheetId,"", new CallBack<PartsRoutingSheet>() {
            @Override
            public void onCompleted(PartsRoutingSheet bean) {
                if (mLoadingDialog!=null&&mLoadingDialog.isShowing())mLoadingDialog.dismiss();
                tvBatchNo.setText(Tools.checkedString(bean.batchNo));
                tvWoNo.setText(Tools.checkedString(bean.woNo));
                tvCmmPN.setText(Tools.checkedString(bean.partno));
                tvIPL.setText(Tools.checkedString(bean.iplNo));
                tvMaterial.setText(Tools.checkedString(bean.material));
                tvPN.setText(Tools.checkedString(bean.pnNHA));
                tvSN.setText(Tools.checkedString(bean.sn));
//                tvProduct.setText(Tools.checkedString(bean.product));
//                tvWoNo.setText(Tools.checkedString(bean.woNo));
                tvNum.setText(Tools.checkedString(bean.number));
                tvDes.setText(Tools.checkedString(bean.description));
                tvRevVersion.setText(Tools.checkedString(bean.revVersion));
                if (bean.manualList != null && bean.manualList.size() > 0) {
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < bean.manualList.size(); i++) {
                        PartsRoutingSheet.ManualListBean listBean = bean.manualList.get(i);
                        if (listBean.disabled)
                            builder.append("<font color='#ff0000' >").append(listBean.revNo).append("</font>");
                        else
                            builder.append("<font color='#333333' >").append(listBean.revNo).append("</font>");
                        if (i != bean.manualList.size() - 1)
                            builder.append("<font color='#ff0000' >,&nbsp</font>");
                    }
                    tvRevNum.setText(Html.fromHtml(builder.toString()));
                } else
                    tvRevNum.setText("");
                sdvPreparedBySign.setImageURI(API.BASE_URL + bean.preparedBySign);
                tvPreparedByDate.setText(Tools.checkedString(bean.preparedDate));
                sdvCheckedBySign.setImageURI(API.BASE_URL + bean.checkedBySign);
                tvCheckedByData.setText(Tools.checkedString(bean.checkedDate));
                sdvApprovedBySign.setImageURI(API.BASE_URL + bean.approvedBySign);
                tvApprovedByDate.setText(Tools.checkedString(bean.approvedDate));

                picAdapter.setNewData(bean.picList);
                adapter.setNewData(bean.stepList);

                if (bean.symbol==null||bean.symbol.size()==0){
                    llKV.setVisibility(View.GONE);
                }else {
                    llKV.setVisibility(View.VISIBLE);
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(CardProcessActivity.this, 5);
                    recyclerViewKV.setLayoutManager(gridLayoutManager);
                    recyclerViewKV.setAdapter(new SymbolAdapter(bean.symbol));
                }

                tvValue0.setText(Tools.checkedString(bean.flangeThickness));
                tvValue1.setText(Tools.checkedString(bean.holeLocation));
                tvValue2.setText(Tools.checkedString(bean.shankLength));
                tvValue3.setText(Tools.checkedString(bean.internalDiameter));
                tvValue4.setText(Tools.checkedString(bean.outerDiameter));
                tvValue5.setText(Tools.checkedString(bean.limitOD));


            }

            @Override
            public void onError() {
                if (mLoadingDialog!=null&&mLoadingDialog.isShowing())mLoadingDialog.dismiss();
            }
        });
    }


    public void showBigPic(ArrayList<String> data, int position) {
        Intent intent = new Intent(this, BigPicActivity.class);
        intent.putExtra("list", data);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}
